//
//  HDIntroPageRootViewController.m
//  healthyexpert
//
//  Created by Liu Penny on 15/1/27.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "HDIntroPageRootViewController.h"

@interface HDIntroPageRootViewController ()

@end

@implementation HDIntroPageRootViewController

//////// self-add/////////

- (NSString* )chooseIntroImagebydevice:(NSString*)pageIndexString
{
    
    NSString * IntroName = Nil;
    
    if(IS_IPHONE5)
    {
        
        IntroName=[pageIndexString stringByAppendingString:@"-568h@2x"];
    }
    else if(IS_IPHONE6)
    {
        IntroName=[pageIndexString stringByAppendingString:@"-667h@2x"];
    }
    else if(IS_IPHONE6p)
    {
        IntroName=[pageIndexString stringByAppendingString:@"-736h@3x"];
    }
    else
    {
        IntroName=[pageIndexString stringByAppendingString:@"@2x"];
    }
    return IntroName;
}
//////////////////////////

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.pageImageprefix = @[@"0",@"1",@"2",@"3"];
    self.pageViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"HDIntroPageViewcontroller"];
    self.pageViewController.dataSource = self;
    self.pageViewController.delegate = self;
    
    [self viewControllerAtIndex:1];
    
   
    NSArray *viewControllers = @[self.IntroDetail];
    [self.pageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:YES completion:nil];
    [self addChildViewController:_pageViewController];
    [self.view addSubview:_pageViewController.view];
    [self.pageViewController didMoveToParentViewController:self];
    
    for (UIView *view in self.pageViewController.view.subviews ) {
        if ([view isKindOfClass:[UIScrollView class]]) {
            UIScrollView *scroll = (UIScrollView *)view;
            scroll.delegate = self;
//            NSLog(@"%f",self.view.frame.size.height);
//            scroll.contentSize = CGSizeMake([self.pageImageprefix count]*self.view.frame.size.width, self.view.frame.size.height);


        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

      if ( self.firstPage && scrollView.contentOffset.x <= scrollView.bounds.size.width+1)

        {
            scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);

           
        }

        else if ( self.lastPage && scrollView.contentOffset.x >= scrollView.bounds.size.width-1) {
            scrollView.contentOffset = CGPointMake(scrollView.bounds.size.width, 0);

        }
    // more
}


- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{

        if (self.firstPage && !self.lastPage && scrollView.contentOffset.x <= scrollView.bounds.size.width+1) {
            velocity = CGPointZero;
            *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        }
        else if (self.lastPage && !self.firstPage && scrollView.contentOffset.x >= scrollView.bounds.size.width-1) {
            velocity = CGPointZero;
            *targetContentOffset = CGPointMake(scrollView.bounds.size.width, 0);
        }

}
- (void)viewControllerAtIndex:(int) index{
    
    if (([self.pageImageprefix count] ==0 )|| (index> [self.pageImageprefix count])) {
        return;
    }
    self.IntroDetail = [self.storyboard instantiateViewControllerWithIdentifier:@"IntroDetailViewController"];
    
    self.IntroDetail.imageFile= [self chooseIntroImagebydevice:[NSString stringWithFormat:@"%d",index]];
    self.IntroDetail.pageIndex= index;
}



- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    int index = ((HDIntroDetailViewController*) viewController).pageIndex;
    if (index < 1) {
        return nil;
    }
    [self viewControllerAtIndex:(((HDIntroDetailViewController*) viewController).pageIndex-1)];
    return self.IntroDetail;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    int index = ((HDIntroDetailViewController*) viewController).pageIndex;
    if (index > [self.pageImageprefix count])
    {
        return nil;
    }

    
    [self viewControllerAtIndex:(((HDIntroDetailViewController*) viewController).pageIndex+1)];
    return self.IntroDetail;
}

- (void)pageViewController:(UIPageViewController *)pageViewController
willTransitionToViewControllers:(NSArray *)pendingViewControllers
{

if (((HDIntroDetailViewController*)(pendingViewControllers[0])).pageIndex == IntroNo){
    
        self.lastPage = true;
        self.firstPage = false;

   }
    else if(((HDIntroDetailViewController*)(pendingViewControllers[0])).pageIndex == 0)
    {
        self.firstPage = true;
        self.lastPage = false;
    }
    else
    {
    self.lastPage = false;
    self.firstPage = false;
    }

}
- (void)pageViewController:(UIPageViewController *)pageViewController
        didFinishAnimating:(BOOL)finished
   previousViewControllers:(NSArray *)previousViewControllers
       transitionCompleted:(BOOL)completed
{
    if(finished && completed)
    {
    self.firstPage = false;
    self.lastPage = false;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
